//
//  CZStatusComment.swift
//  GZWeibo666
//
//  Created by 吴先生 on 15/12/1.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

class CZDetailCommentModel:NSObject
{
    //评论创建时间
    var created_at: String?
    //评论的ID
    var id : Int = 0
    //评论的内容
    var text : String?
    //评论的来源
    var source : String?
    //评论作者的用户信息字段 详细
    var user : CZUser?
    //评论的MID
    var mid : String?
    //字符串型的评论ID
    var idstr : String?
    //评论的微博信息字段 详细
    var status : NSObject?
    //评论来源评论，当本评论属于对另一评论的回复时返回此字段
    var reply_comment : NSObject?
    
    var loading = false
    
    init(loading: Bool){
        self.loading = loading
        super.init()
    }

   
    //字典转模型
    init(dict:[String: AnyObject])
    {
        super.init()
        //KVC字典转模型
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user"{
            if let dict = value as? [String:AnyObject]{
                user = CZUser(dict:dict)
            }
            return
        }
        super.setValue(value, forKey: key)
    }
    
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    override  var description: String{
        let properties = ["id","created_at","user"]
        return "\n\t:评论模型:\(dictionaryWithValuesForKeys(properties))"
    }
    
    class func loadComment(statusId:Int,since_id:Int,max_id:Int,finished:(comment:[CZDetailCommentModel]?,total_number:Int?,pervious_cursor:Int?,next_cursor:Int?,error:NSError?)->()){
        CZNetworkTool.sharedInstance.loadStatusComment(statusId, since_id: since_id, max_id: max_id) { (result, error) -> () in
            if error != nil{
                print("请求评论失败:\(error)")
                finished(comment: nil, total_number: nil, pervious_cursor: nil , next_cursor: nil, error: error)
                return
            }
            
            print("result:\(result)")
            
            //总数量
            let total_number = result?["total_number"] as? Int
            
            //上一页id
            let previous_cursor = result?["previous_cursor"] as? Int
            
            //下一页id
            let next_cursor = result?["next_cursor"] as? Int
            
            //字典转模型
            var comments = [CZDetailCommentModel]()
            
            //解析comments 里面的评论模型
            if let array = result?["comments"] as? [[String:AnyObject]]{
                for dict in array{
                    let comment = CZDetailCommentModel(dict:dict)
                    
                    comments.append(comment)
                }
                
                finished(comment: comments, total_number: total_number, pervious_cursor: previous_cursor, next_cursor: next_cursor, error: nil)
            }else{
                finished(comment: nil, total_number: total_number, pervious_cursor:previous_cursor , next_cursor: next_cursor, error: nil)
            }
            
        }
        
    }
    
//    func cellId() -> String {
//        return retweeted_status == nil ? CZStatusReuseIdentifier.NormalCell.rawValue : CZStatusReuseIdentifier.ForwardCell.rawValue
//    }
}



